package com.hnhegui.backend.platform.service.order;

import com.hnhegui.backend.entity.PageResult;
import com.hnhegui.backend.entity.Result;
import com.hnhegui.backend.order.api.OrderRestApi;
import com.hnhegui.backend.order.dto.request.AdminOrdersRequestDTO;
import com.hnhegui.backend.order.dto.request.OrderDocumentPageRequestDTO;
import com.hnhegui.backend.order.dto.response.CreateOrderResponseDTO;
import com.hnhegui.backend.order.dto.response.OrderDocumentResponseDTO;
import com.hnhegui.backend.platform.convert.order.OrderConvert;
import com.hnhegui.backend.platform.vo.order.request.CreateOrderRequestVO;
import com.hnhegui.backend.platform.vo.order.response.CreateOrderResponseVO;
import com.hnhegui.backend.platform.vo.order.response.OrderDocumentResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author 刘大海
 * @since 2025/10/27 11:05
 */
@Service
public class RestOrderService {

    @Autowired
    private OrderRestApi orderRestApi;

    public CreateOrderResponseVO createOrder(CreateOrderRequestVO request) {
        Result<CreateOrderResponseDTO> order =
            orderRestApi.createOrder(OrderConvert.INSTANCE.convertCreateOrderRequest(request));
        return OrderConvert.INSTANCE.convertCreateOrderResponse(order.getData());
    }

    public PageResult<OrderDocumentResponseVO> getUserOrders(String userId,
                                                             String status,
                                                             LocalDateTime startTime,
                                                             LocalDateTime endTime,
                                                             Integer page, Integer pageSize) {
        OrderDocumentPageRequestDTO request = new OrderDocumentPageRequestDTO();
        request.setUserId(userId);
        request.setStatus(status);
        request.setStartTime(startTime);
        request.setEndTime(endTime);
        if (page != null) {
            request.setPage(page);
        }
        if (pageSize != null) {
            request.setPageSize(pageSize);
        }
        Result<PageResult<OrderDocumentResponseDTO>> myOrders = orderRestApi.getMyOrders(request);
        PageResult<OrderDocumentResponseDTO> orderPage = myOrders.getData();
        return new PageResult<>(orderPage.getTotal(), orderPage.getItems().stream().map(OrderConvert.INSTANCE::convertOrderDocument).toList());
    }

    public OrderDocumentResponseVO getOrderById(String orderId) {
        Result<OrderDocumentResponseDTO> order = orderRestApi.getOrderDetail(orderId);
        return OrderConvert.INSTANCE.convertOrderDocument(order.getData());
    }

    public PageResult<OrderDocumentResponseVO> searchAdminOrders(String orderNo,
                                                                 String userId,
                                                                 String productName,
                                                                 String receiverPhone,
                                                                 String status,
                                                                 String paymentStatus,
                                                                 LocalDateTime startTime,
                                                                 LocalDateTime endTime,
                                                                 Integer page,
                                                                 Integer pageSize) {
        AdminOrdersRequestDTO request = new AdminOrdersRequestDTO();
        request.setOrderNo(orderNo);
        request.setUserId(userId);
        request.setProductName(productName);
        request.setReceiverPhone(receiverPhone);
        request.setStatus(status);
        request.setPaymentStatus(paymentStatus);
        request.setStartTime(startTime);
        request.setEndTime(endTime);
        request.setPage(page);
        request.setPageSize(pageSize);
        Result<PageResult<OrderDocumentResponseDTO>> adminOrders = orderRestApi.getAdminOrders(request);
        PageResult<OrderDocumentResponseDTO> orderPage = adminOrders.getData();
        return new PageResult<>(orderPage.getTotal(), orderPage.getItems().stream().map(OrderConvert.INSTANCE::convertOrderDocument).toList());
    }
}
